clear all
cd "C:\Users\favero\Documents\COVID-19\replication materials\data & analysis"

capture log close
log using covid19analysis.txt, text replace

// Note: requires installation of user-written package "estout"

use covid19perceptions


*** Initial Description and Item Construction/Data Cleaning ***

// same user took survey twice; deleting 2nd response
drop if ResponseId == "R_1FJTuRyeH7msWDG"

// check how many respondents fail attention checks
gen countattention = (attention1==2) + (attention2_1==0) + (attention3_1==10)
tab countattention

// Main independent variable
rename arm arm_str
encode arm_str, gen(arm)
label define arm 1 "Control" 2 "Treat. 1" 3 "Treat. 2" 4 "Treat. 3" 5 "Treat. 4", replace

// Dependent variable 1
egen maxweeks = rowmean(maxweeks?)
replace maxweeks = 104 if maxweeks > 104 // windsorize 12 extreme values
la var maxweeks "# of Weeks Willing to Isolate"

// Dependent variable 2
alpha socdist?_1
gen socdist = (10-socdist1_1) + socdist2_1 + (10-socdist3_1) + socdist4_1 + socdist5_1
la var socdist "Intended Social Distancing Index"
la var socdist1_1 "Item 1"
la var socdist2_1 "Item 2"
la var socdist3_1 "Item 3"
la var socdist4_1 "Item 4"
la var socdist5_1 "Item 5"

// Attitudes/Beliefs
la var attitude1_1 "Biggest Threat"
la var attitude2_1 "Reduce Deaths"
la var attitude3_1 "Protect Economy"
la var attitude4_1 "Close Business"

// Descriptive stats
sum maxweeks socdist, detail
tab socdist
tab arm
sum attitude?_1

// Figure 1
hist maxweeks, freq name(dv1, replace) scheme(s1mono)
hist socdist, freq name(dv2, replace) scheme(s1mono)
graph combine dv1 dv2, scheme(s1mono) col(1) ycommon ysize(5) xsize(4)
graph export histograms.png, replace width(1500)

// Controls: Gender
replace gender = . if gender == -9
recode gender (1=0) (2=1) (3=.), gen(female)

// Controls: Age
la var age "Age (10-Yr. Increments)"
recode age (1/3 = 0) (4/8 = 1), gen(over34)

// Controls: Race
egen racechoices = rowtotal(race_?)
gen white = 0 if racechoices > 0
replace white = 1 if race_1==1 & racechoices==1
replace white = . if race__9==1
la var white "White"
tab white

// Controls: Education
la var edu "Education"
recode edu (1/4=0) (5/8=1), gen(bachelor)

// Controls: Partisanship
la var party "Party"
label define labels11 1 "Republican" 2 "Democrat" 3 "Other (Party)", replace
recode party (1=0) (2=1) (3=.), gen(democrat)

// Controls: News consumption
recode follownews (1/3=0) (4=1), gen(verynewsy)
la var verynewsy "Heavy News Consumer"

// Controls: Prosocial motivation
gen prosoc = motiv2 + motiv3 + motiv4 + motiv5
alpha motiv2 motiv3 motiv4 motiv5
tab prosoc
recode prosoc (4/16 = 0) (17/20 = 1), gen(veryprosoc)
tab prosoc veryprosoc
la var veryprosoc "High Prosocial Mot."

// Controls: Emapthy
recode motiv1 (1/4=0) (5=1), gen(veryempath)
tab motiv1 veryempath
la var veryempath "High Empathy"


***** New stuff for revision
sureg (maxweeks i.arm) (socdist i.arm)

anova maxweeks arm
pwcompare arm, effects mcompare(dunnett)

anova socdist arm
pwcompare arm, effects mcompare(dunnett)



*** Analysis ***

// DV1: Duration of social distancing
graph box maxweeks, over(arm) scheme(s1mono) // Figure 2
graph export maxweeks.png, replace width(1500)
oneway maxweeks arm, tab bonferroni // main model
oneway maxweeks arm if maxweeks <= 56, tab bonferroni // main model robust to dropping outliers
reg maxweeks i.arm, robust level(90) // robustness check, Table A-1; note: 90% CIs were manually added to the tables in paper
estimates store dv1, title("Duration")
estadd scalar p_F = Ftail(e(df_m), e(df_r), e(F))
reg maxweeks i.arm i.gender age white edu, robust // robustness check, Table A-1
estimates store dv1_controls, title("Duration")
estadd scalar p_F = Ftail(e(df_m), e(df_r), e(F))
reg maxweeks i.arm if maxweeks <= 56, robust // robustness check, Table A-1
estimates store dv1_nooutliers, title("Duration, w/o Outliers")
estadd scalar p_F = Ftail(e(df_m), e(df_r), e(F))
kwallis maxweeks, by(arm) // sensitivity analysis

// DV2: Index of intended social distancing behaviors
graph box socdist, over(arm) scheme(s1mono) // Figure 3
graph export socdist.png, replace width(1500)
oneway socdist arm, tab bonferroni // main model
reg socdist i.arm, robust level(90) // robustness check, Table A-1; note: 90% CIs were manually added to the tables in paper
estimates store dv2, title("Behavior Index")
estadd scalar p_F = Ftail(e(df_m), e(df_r), e(F))
reg socdist i.arm i.gender age white edu, robust // robustness check, Table A-1
estimates store dv2_controls, title("Behavior Index")
estadd scalar p_F = Ftail(e(df_m), e(df_r), e(F))
reg socdist1_1 i.arm, robust // robustness check, Table A-2
estimates store dv2_1
estadd scalar p_F = Ftail(e(df_m), e(df_r), e(F))
reg socdist2_1 i.arm, robust // robustness check, Table A-2
estimates store dv2_2
estadd scalar p_F = Ftail(e(df_m), e(df_r), e(F))
reg socdist3_1 i.arm, robust // robustness check, Table A-2
estimates store dv2_3
estadd scalar p_F = Ftail(e(df_m), e(df_r), e(F))
reg socdist4_1 i.arm, robust // robustness check, Table A-2
estimates store dv2_4
estadd scalar p_F = Ftail(e(df_m), e(df_r), e(F))
reg socdist5_1 i.arm, robust // robustness check, Table A-2
estimates store dv2_5
estadd scalar p_F = Ftail(e(df_m), e(df_r), e(F))
kwallis socdist, by(arm) // sensitivity analysis

// Multivariate regression (sensitivity analysis)
mvreg maxweeks socdist = i.arm, corr
estimates store multivar

// Heterogeneous effects: demographics
reg maxweeks i.arm##1.female, robust // Table A-3
estimates store dv1_female, title("Female")
estadd scalar p_F = Ftail(e(df_m), e(df_r), e(F))
reg socdist i.arm##1.female, robust // Table A-3
estimates store dv2_female, title("Female")
estadd scalar p_F = Ftail(e(df_m), e(df_r), e(F))
reg maxweeks i.arm##1.over34, robust // Table A-3
estimates store dv1_over34, title("35+")
estadd scalar p_F = Ftail(e(df_m), e(df_r), e(F))
reg socdist i.arm##1.over34, robust // Table A-3
estimates store dv2_over34, title("35+")
estadd scalar p_F = Ftail(e(df_m), e(df_r), e(F))
reg maxweeks i.arm##1.white, robust // Table A-3
estimates store dv1_white, title("White")
estadd scalar p_F = Ftail(e(df_m), e(df_r), e(F))
reg socdist i.arm##1.white, robust // Table A-3
estimates store dv2_white, title("White")
estadd scalar p_F = Ftail(e(df_m), e(df_r), e(F))
reg maxweeks i.arm##1.bachelor, robust // Table A-3
estimates store dv1_bachelor, title("College")
estadd scalar p_F = Ftail(e(df_m), e(df_r), e(F))
reg socdist i.arm##1.bachelor, robust // Table A-3
estimates store dv2_bachelor, title("College")
estadd scalar p_F = Ftail(e(df_m), e(df_r), e(F))

// Heterogeneous effects: attitudes/behaviors
reg maxweeks i.arm##1.democrat, robust // Table A-4
estimates store dv1_democrat, title("Democrat")
estadd scalar p_F = Ftail(e(df_m), e(df_r), e(F))
reg socdist i.arm##1.democrat, robust // Table A-4
estimates store dv2_democrat, title("Democrat")
estadd scalar p_F = Ftail(e(df_m), e(df_r), e(F))
reg maxweeks i.arm##1.verynewsy, robust // Table A-4
estimates store dv1_verynewsy, title("News")
estadd scalar p_F = Ftail(e(df_m), e(df_r), e(F))
reg socdist i.arm##1.verynewsy, robust // Table A-4
estimates store dv2_verynewsy, title("News")
estadd scalar p_F = Ftail(e(df_m), e(df_r), e(F))
reg maxweeks i.arm##1.veryprosoc, robust // Table A-4
estimates store dv1_veryprosoc, title("Prosoc.")
estadd scalar p_F = Ftail(e(df_m), e(df_r), e(F))
reg socdist i.arm##1.veryprosoc, robust // Table A-4
estimates store dv2_veryprosoc, title("Prosoc.")
estadd scalar p_F = Ftail(e(df_m), e(df_r), e(F))
reg maxweeks i.arm##1.veryempath, robust // Table A-4
estimates store dv1_empath, title("Empathy")
estadd scalar p_F = Ftail(e(df_m), e(df_r), e(F))
reg socdist i.arm##1.veryempath, robust // Table A-4
estimates store dv2_empath, title("Empathy")
estadd scalar p_F = Ftail(e(df_m), e(df_r), e(F))

// DV3: Attitudes/Beliefs
oneway attitude1_1 arm, tab bonferroni
reg attitude1_1 i.arm, robust // Table A-5
estimates store dv3_1
estadd scalar p_F = Ftail(e(df_m), e(df_r), e(F))
oneway attitude2_1 arm, tab bonferroni
reg attitude2_1 i.arm, robust // Table A-5
estimates store dv3_2
estadd scalar p_F = Ftail(e(df_m), e(df_r), e(F))
oneway attitude3_1 arm, tab bonferroni
reg attitude3_1 i.arm, robust // Table A-5
estimates store dv3_3
estadd scalar p_F = Ftail(e(df_m), e(df_r), e(F))
oneway attitude4_1 arm, tab bonferroni
reg attitude4_1 i.arm, robust // Table A-5
estimates store dv3_4
estadd scalar p_F = Ftail(e(df_m), e(df_r), e(F))

// Balance tests
oneway female arm, tab
oneway age arm, tab
oneway white arm, tab
oneway edu arm, tab

*** Export results to tables ***

esttab dv1 dv1_controls dv1_nooutliers dv2 dv2_controls, ///
   title(Table A-1. Effects on Social Distancing) /// 
   mlabels(,titles) cells(b(fmt(3) star) se(par fmt(3))) ///
   legend starlevels(* 0.05 ** 0.01) label ///
   stats(F p_F r2 N, fmt(3 3 3 0) label("F-statistic" "F p-value" "R-sqr" N)) ///
   drop(1.arm 1.gender) ///
   addnotes("Robust standard errors in parentheses." "For models 1-3, the dependent variable indicates the maximum number of weeks the respondent can imagine themself isolating. For models 4-5, the dependent variable is an index of intended social distancing behaviors." "In model 3, all observations with values of the dependent variable greater than 52 (1 year) are dropped.") ///
   , using "tables.rtf", replace

la var maxweeks "Duration"
la var socdist "Dist. Index"
esttab dv2_1 dv2_2 dv2_3 dv2_4 dv2_5 multivar, ///
   title(Table A-2. Effects on Individual Items from Social Distancing Behavior Index) /// 
   cells(b(fmt(3) star) se(par fmt(3))) ///
   legend starlevels(* 0.05 ** 0.01) label ///
   stats(F p_F r2 N, fmt(3 3 3 0) label("F-statistic" "F p-value" "R-sqr" N)) ///
   drop(1.arm) ///
   unstack ///
   addnotes("Robust standard errors in parentheses.") ///
   , using "tables.rtf", append

esttab dv1_female dv1_over34 dv1_white dv1_bachelor dv2_female dv2_over34 dv2_white dv2_bachelor, ///
   title(Table A-3. Heterogeneous Effects by Demographic Characteristics) /// 
   mlabels(,titles) cells(b(fmt(3) star) se(par fmt(3))) ///
   mgroups("DV: Duration" "DV: Behavior Index", pattern(1 0 0 0 1 0 0 0)) ///
   legend starlevels(* 0.05 ** 0.01) label ///
   stats(F p_F r2 N, fmt(3 3 3 0) label("F-statistic" "F p-value" "R-sqr" N)) ///
   rename(1.over34 1.female  2.arm#1.over34 2.arm#1.female  3.arm#1.over34 3.arm#1.female  4.arm#1.over34 4.arm#1.female  5.arm#1.over34 5.arm#1.female ///
	1.white 1.female  2.arm#1.white 2.arm#1.female  3.arm#1.white 3.arm#1.female  4.arm#1.white 4.arm#1.female  5.arm#1.white 5.arm#1.female ///
	1.bachelor 1.female  2.arm#1.bachelor 2.arm#1.female  3.arm#1.bachelor 3.arm#1.female  4.arm#1.bachelor 4.arm#1.female  5.arm#1.bachelor 5.arm#1.female ) ///
   keep(2.arm 3.arm 4.arm 5.arm 1.female 2.arm#1.female 3.arm#1.female 4.arm#1.female 5.arm#1.female) ///
   varlabels(1.female "Mediator" 2.arm#1.female "Med. X T1" 3.arm#1.female "Med. X T2" 4.arm#1.female "Med. X T3" 5.arm#1.female "Med. X T4") ///
   addnotes("Robust standard errors in parentheses." "Moderator variable for each model listed below model number.") ///
   , using "tables.rtf", append

esttab dv1_democrat dv1_verynewsy dv1_veryprosoc dv1_empath dv2_democrat dv2_verynewsy dv2_veryprosoc dv2_empath, ///
   title(Table A-4. Heterogeneous Effects by Other Attitudes/Behaviors) /// 
   mlabels(,titles) cells(b(fmt(3) star) se(par fmt(3))) ///
   mgroups("DV: Duration" "DV: Behavior Index", pattern(1 0 0 0 1 0 0 0)) ///
   legend starlevels(* 0.05 ** 0.01) label ///
   stats(F p_F r2 N, fmt(3 3 3 0) label("F-statistic" "F p-value" "R-sqr" N)) ///
   rename(1.verynewsy 1.democrat  2.arm#1.verynewsy 2.arm#1.democrat  3.arm#1.verynewsy 3.arm#1.democrat  4.arm#1.verynewsy 4.arm#1.democrat  5.arm#1.verynewsy 5.arm#1.democrat ///
	1.veryprosoc 1.democrat  2.arm#1.veryprosoc 2.arm#1.democrat  3.arm#1.veryprosoc 3.arm#1.democrat  4.arm#1.veryprosoc 4.arm#1.democrat  5.arm#1.veryprosoc 5.arm#1.democrat ///
	1.veryempath 1.democrat  2.arm#1.veryempath 2.arm#1.democrat  3.arm#1.veryempath 3.arm#1.democrat  4.arm#1.veryempath 4.arm#1.democrat  5.arm#1.veryempath 5.arm#1.democrat ) ///
   keep(2.arm 3.arm 4.arm 5.arm 1.democrat 2.arm#1.democrat 3.arm#1.democrat 4.arm#1.democrat 5.arm#1.democrat) ///
   varlabels(1.democrat "Mediator" 2.arm#1.democrat "Med. X T1" 3.arm#1.democrat "Med. X T2" 4.arm#1.democrat "Med. X T3" 5.arm#1.democrat "Med. X T4") ///
   addnotes("Robust standard errors in parentheses." "Moderator variable for each model listed below model number.") ///
   , using "tables.rtf", append

esttab dv3_1 dv3_2 dv3_3 dv3_4, ///
   title(Table A-5. Effects on COVID-19-Related Attitudes and Beliefs) /// 
   cells(b(fmt(3) star) se(par fmt(3))) ///
   legend starlevels(* 0.05 ** 0.01) label ///
   stats(F p_F r2 N, fmt(3 3 3 0) label("F-statistic" "F p-value" "R-sqr" N)) ///
   drop(1.arm) ///
   addnotes("Robust standard errors in parentheses.") ///
   , using "tables.rtf", append


log close

